import hashlib
from typing import Union, List, Dict

from pydantic import Field, BaseModel

from backend.app.schemas.base import SchemaBase
from backend.app.schemas.device_v2 import TenderDeviceReqV2


class DeviceCommonSchema(SchemaBase):
    """设备基础信息"""
    gid: int | str = Field(description="设备ID", alias="g_id", default=0)
    sid: int | str = Field(description="商户ID", default=0)
    device_name: str = Field(description="设备名称", default="")
    device_avatar: str = Field(description="图片地址", alias="picPath", default="")
    catena: str = Field(description="系列", alias="mod1", default="")
    mod: str = Field(description="型号", default="")
    brand_id: int = Field(description="品牌ID", default=0)
    brand_title: str = Field(description="品牌名称", alias="brandTitle", default="")
    class_id: int = Field(description="三级分类ID", default=0)
    class_name: str = Field(description="三级分类", default="")
    class_1_id: int = Field(description="一级分类ID", default=0)
    class_1_name: str = Field(description="一级分类", default="")
    class_2_id: int = Field(description="二级分类ID", default=0)
    class_2_name: str = Field(description="二级分类", default="")
    price: float | str = Field(description="价格", default=0)
    meter: str = Field(description="技术参数", default="")
    meter_clean: str = Field(description="格式化后的设备属性", default="")
    spare_meter: str = Field(description="设备参数", default="")
    address: str = Field(description="店铺地址", default="")
    meter_coding: str = Field(description="物料编码", default="")
    country_title: str = Field(description="国别", alias="countryTitle", default="")
    country: str | float = Field(description="国别标识", alias="country", default="")

    def to_page_content(self) -> str:
        """转换为向量描述"""
        vector_desc = f"""
        设备ID:{self.gid}, 一级分类:{self.class_1_name}, 二级分类:{self.class_2_name}, 三级分类:{self.class_name}, 品牌：{self.brand_title}, 
        系列:{self.catena}, 型号:{self.mod}, 国别:{self.country_title}, 物料编码：{self.meter_coding}, 设备名称: {self.device_name}, 
        店铺地址：{self.address}, 设备属性：{self.meter_clean or self.meter}
        """

        return vector_desc


class DeviceInfoSchema(DeviceCommonSchema):
    """设备基础属性"""
    # goods_id: int = Field(description="设备ID", default=0)
    vector_id: str = Field(description="向量ID", default="")
    child_list: List = Field(description="相似设备", default=[])


class DeviceDetailReq(BaseModel):
    """获取商品详情"""
    device_name: str = Field(description="标题")
    gid: int = Field(description="产品ID", alias="g_id")
    vector_id: str | None = Field(description="向量ID", default=None)


class DeviceSearchReq(BaseModel):
    """设备列表搜索"""
    g_ids: List[int] = Field(description="设备ID列表", default=[])
    device_name: str = Field(description="设备名称", default="")


class DeviceDeleteReq(BaseModel):
    """删除商品向量"""
    vector_ids: List[str] = Field(description="商品对应的向量ID")
    device_ids: List[int] = Field(description="商品ID列表", default=[])


class TenderDeviceReq(DeviceCommonSchema):
    """【标书】搜索设备请求参数"""
    child_system: str = Field(description="子系统", default="")
    number: int | str = Field(description="数量", default=1)
    unit: str = Field(description="计量单位", default="")
    key: int | str = Field(description="顺序", default=0)
    supply_price_b: Union[int, float, str, None] = Field(description="参考价格", default=0)
    price_list: Dict = Field(description="价格属性", alias="priceList", default={})
    tender_name: str = Field(description="标书设备名称", default="")
    tender_meter: str = Field(description="标书设备参数要求")
    child_list: List = Field(description="相似设备", default=[])

    def hash_id(self):
        """清单问题标识"""
        name = self.tender_name or self.device_name or ""
        meter = self.tender_meter or ""
        encoded_string = (name + meter).encode('utf-8')
        return hashlib.md5(encoded_string).hexdigest()

    def __str__(self) -> str:
        return f"<{self.__class__.__name__}>"


class TenderModReq(SchemaBase):
    """标书请求参数-模块"""
    id: int | str = Field(description="子模块ID", default=0)
    mould_name: str = Field(description="子模块名称", default="")
    sort: int = Field(description="模块排序", default=0)
    device_single_rows: List = Field(description="设备标签列表", default=[])
    device_list: List[TenderDeviceReqV2] = Field(description="设备要求列表", alias="deviceList", default=[])


class TenderSystemReq(SchemaBase):
    """标书系统请求参数"""
    price: float = Field(description="参考价格", alias="xtPrice", default=0)
    goods: List[TenderModReq] = Field(description="模块列表", default=[])


class TenderReq(SchemaBase):
    """标书匹配参数"""
    device_list: List[TenderSystemReq] = Field(description="子模块列表", alias="deviceList", default=[])
    price_name: str = Field(description="价格字段名称", alias="priceName", default="")
    sid: int = Field(description="商户ID", default=0)
    country: int = Field(description="国别排序(0: 无, 1：国产优先 2：进口优先)", default=0)
    platform: str = Field(description="平台名称(avl:平台，hushan:湖山)", default="avl")


class PageContentSchema(SchemaBase):
    """向量数据库字段"""
    gid: int = Field(description="设备ID", alias="g_id")
    class_name: str = Field(description="二级分类", default="")
    device_name: str = Field(description="名称", default="")
    countryTitle: str = Field(description="所属国家", default="")
    spare_meter: str = Field(description="技术参数", default="")
    meter: str = Field(description="设备属性", default="")

    def to_page_content(self) -> str:
        """转换为向量描述"""
        # "g_id", "class_name", "device_name", "countryTitle", "meter", "spare_meter"
        lists = [self.gid, self.class_name, self.device_name, self.meter, self.spare_meter]
        #  去除空属性
        lists = [str(x) for x in lists if x]
        return ";".join(lists)


class TenderVectorSchema(BaseModel):
    """RAG增强搜索--->结果匹配字段"""
    # "g_id", "class_name", "device_name", "countryTitle", "meter", "spare_meter"
    g_id: str = Field(description="设备id,是一个数字", default="")
    class_name: str = Field(description="设备分类，是一个系列的统称", default="")
    device_name: str = Field(description="名称", default="")
    countryTitle: str = Field(description="所属国家", default="")
    # spare_meter: str = Field(description="设备技术参数", default="")
    # meter: str = Field(description="设备参数值", default="")


class DeviceAttributeSchema(BaseModel):
    """设备属性字段"""
    # des: str = Field(description="单独描述的设备属性", default="")
    attribute: str = Field(description="attribute", default="")
    value: str = Field(description="value", default="")
    # tag: str = Field(description="tag", default="")


class DeviceAttributeListSchema(BaseModel):
    """设备属性列表"""
    lists: List[DeviceAttributeSchema]
